package com.argticling.dao.impl;

import com.argticling.dao.impl.hibernate.HibernateUtil;
import com.argticling.model.ImageArticleFragment;

import java.util.List;

public class ImageFragmentDAOImplementacion extends AbstractDAOImplementation {

    public void commitModifiedImageFragments(List<ImageArticleFragment> textFragments, String id) {
        for (ImageArticleFragment textFrag : textFragments) {
            ImageArticleFragment oldTextFrag = getImageFragmentById(textFrag.getId());
            if (textFrag.hashCode() == (oldTextFrag.hashCode())) {
                new HibernateUtil().persist(textFrag);
            }
        }
    }

    public void commitAllTextFragments(List<ImageArticleFragment> textFragments, String id) {
        for (ImageArticleFragment textFrag : textFragments) {
            new HibernateUtil().persist(textFrag);
        }
    }

    public List<ImageArticleFragment> getImageFragmentsByArticleId(String id) {
        List<ImageArticleFragment> imageFrag = new HibernateUtil().getSession().createQuery("FROM ImageArticleFragment as imageFrag where " +
                "imageFrag.articleID = ?").setString(0, id).list();
        return imageFrag;
    }

    public ImageArticleFragment getImageFragmentById(String id) {
        ImageArticleFragment imageFrag = (ImageArticleFragment) new HibernateUtil().getSession().createQuery("FROM ImageArticleFragment as imageFrag where " +
                "imageFrag.id = ?").setString(0, id).uniqueResult();
        return imageFrag;
    }
}
